package com.time.hellotime.db;

import android.app.Activity;

import com.time.hellotime.app.MyApplication;
import com.time.hellotime.db.dao.impl.DynamicsDao;
import com.time.hellotime.db.dao.impl.PicDdao;
import com.time.hellotime.db.dao.impl.UserDao;
import com.time.hellotime.db.model.DynamicsModel;
import com.time.hellotime.db.model.PicModel;
import com.time.hellotime.db.model.UserModel;
import com.time.hellotime.servermodel.User;
import com.time.hellotime.servermodel.dynamics.Dynamics;
import com.time.hellotime.viewmodel.DynamicsViewModel;
import com.time.hellotime.viewmodel.UserViewModel;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 处理ViewModel
 * Created by Bing on 2016/8/24.
 * E-mail is tx16bing@163.com
 */
public class ViewModelCreator {

    private static ViewModelCreator viewModelCreator;
    private ModelCreator modelCreator;
    private MyApplication application;

    private ViewModelCreator(MyApplication application) {
        this.application = application;
        this.modelCreator = ModelCreator.getInstance(application);
    }

    public static synchronized ViewModelCreator getInstance(Activity activity) {

        if (viewModelCreator == null) {
            viewModelCreator = new ViewModelCreator((MyApplication) activity.getApplication());
        }
        return viewModelCreator;
    }

    public static synchronized ViewModelCreator getInstance(MyApplication application) {

        if (viewModelCreator == null) {
            viewModelCreator = new ViewModelCreator(application);
        }
        return viewModelCreator;
    }

    public UserViewModel setUserViewModel(User user) {
        UserModel userModel = modelCreator.setUserModel(user);
        return new UserViewModel(application, userModel);
    }

    public UserViewModel getUserViewModel() {
        UserViewModel userViewModel = new UserViewModel();
        List<UserModel> userModels = new UserDao().find();
        if (userModels != null) {
            for (UserModel userModel : userModels) {
                userViewModel = new UserViewModel(application, userModel);
            }
        }
        return userViewModel;
    }

    // 保存动态列表
    public List<DynamicsViewModel> setDynamicsViewModels(List<Dynamics> dynamicses) {
        List<DynamicsViewModel> dynamicsViewModels = new ArrayList<>();
        // 保存新数据前清空表中所有缓存
        DataSupport.deleteAll(DynamicsModel.class);
        for (Dynamics dynamics : dynamicses) {
            DynamicsModel dynamicsModel = modelCreator.setDynamicsModel(dynamics);
            dynamicsViewModels.add(new DynamicsViewModel(application, dynamicsModel));
        }
        Collections.sort(dynamicsViewModels);
        return dynamicsViewModels;
    }

    //获取动态列表
    public List<DynamicsViewModel> getDynamicsViewModels() {
        List<DynamicsViewModel> dynamicsViewModels = new ArrayList<>();
        List<DynamicsModel> dynamicsModels = new DynamicsDao().find();
        if (dynamicsModels != null) {
            for (DynamicsModel dynamicsModel : dynamicsModels) {
                // 根据publishId获取对应的图片
                List<PicModel> picModels = new PicDdao().find(dynamicsModel.getDynamicsId());
                DynamicsViewModel dynamicsViewModel = new DynamicsViewModel(application, dynamicsModel, picModels);
                dynamicsViewModels.add(dynamicsViewModel);
            }
        }
        Collections.sort(dynamicsViewModels);
        return dynamicsViewModels;
    }

}

